package myapp.DAO;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

import myapp.DTO.ServiceDTO;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import staticClass.MyRequest;
import android.util.Log;

public class ServiceDAO extends DataProvider{
	private final String url = "Service/";
	
	public ArrayList<ServiceDTO> getAllServices(){
		String strResponse = requestGET(dbBaseHost + url);
		
		return parseJSONResponToArr(strResponse);
	}
	
	public ArrayList<ServiceDTO> getServicesByKeyWord(String keyword){
		String encodedKey = null;
		String charset = HTTP.UTF_8;
		try {
			encodedKey = URLEncoder.encode(keyword, charset);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String getQuery = dbBaseHost + url + "?id=" +encodedKey;
		Log.i("ServiceDAO", "[getServicesByKeyWord] requestUrl:" + getQuery);
		String strResponse = requestGET(getQuery);
		return parseJSONResponToArr(strResponse);
	}
	
	public ArrayList<ServiceDTO> getServicesInDistance(double yourLat, double yourLong, double distance){
		String query = "?yourLat=" + yourLat +
				"&yourLong=" + yourLong +
				"&distance=" + distance;
		String strResponse = requestGET(dbBaseHost + url + query);
		
		return parseJSONResponToArr(strResponse);
	}
	
	public void AddNewService(ServiceDTO service){
		try {
			JSONObject json = service.toJson();
			String serverURL = dbBaseHost + "service";
			MyRequest.postDataJson(serverURL, json);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private ServiceDTO jsonToServiceDTO(JSONObject json){
		try {
			return new ServiceDTO(json.getInt("Id"),
					json.getString("ServiceName"),
					json.getInt("Category"),
					json.getDouble("Latitude"),
					json.getDouble("Longtitude"),
					json.getString("Address"),
					json.getString("Phone"),
					json.getString("Description"));
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return null;
	}
	
	private ServiceDTO parseJsonResponseToObj(String strResponse){
		JSONObject json = null;
		Log.i("ServiceDAO", "[parseJsonResponseToObj] jsonStr: " + strResponse);
		
		try {
			json = new JSONObject(strResponse);
			Log.i("ServiceDAO", "[parseJsonResponseToObj] json = " + json.toString());
		} catch (Exception e) {
			// TODO: handle exception
			Log.i("ServiceDAO", "[parseJsonResponseToObj] Exception: " + e.getMessage());
		}
		if (json != null)
			return jsonToServiceDTO(json);
		
		return null;
	}
	
	private ArrayList<ServiceDTO> parseJSONResponToArr(String jsonResponse){
		JSONArray jsons;
		ArrayList<ServiceDTO> arr = new ArrayList<ServiceDTO>();
		
		Log.i("ServiceDAO", "[parseJSONArr] jsonStr: " + jsonResponse);
		
		try {
			jsons = new JSONArray(jsonResponse);
			Log.i("ServiceDAO", "[parseJSONArr] jsonsLength = " + jsons.length());
			
			for (int i=0; i<jsons.length(); i++){
				arr.add(jsonToServiceDTO(jsons.getJSONObject(i)));
			}						
		} catch (Exception e) {
			// TODO: handle exception
			Log.i("ServiceDAO", "[parseJSONArr] Exception: " + e.getMessage());
		}		
		
		return arr;
	}
}
